import { getMenu } from '@/api/common/static'
const state = {
  init: false,
  noticeTypesArray: null,
  noticeTypesDict: null,
  noticeTypes: null,
  documentTypes: null,
  documentTypesArray: null,
  documentTypesDict: null,
}

const actions = {
  initDictionary({ state }) {
    return new Promise((res, rej) => {
      if (state.init) return res(state)
      state.init = true
      const nameArray = ['party.notice', 'party.document']
      Promise.all([getMenu(nameArray[0]), getMenu(nameArray[1])]).then(datas => {
        const toHandleList = ['noticeTypes', 'documentTypes']
        toHandleList.map((name, index) => {
          state[name] = datas[index] && datas[index].list.filter(i => i.name !== nameArray[index])
          const key = `${name}Array`
          state[key] = state[name].map(i => { return i.name })
          const dictKey = `${name}Dict`
          const dict = {}
          state[dictKey] = dict
          state[name].map(i => { dict[i.name] = i })
        })
        res(state)
      }).catch(e => {
        state.init = false // 重置加载状态
        rej(e)
      })
    })
  }
}

export default {
  state, actions, namespaced: true
}
